<template>
  <div class="pagestyle">
    <list_controller
    :showCaozuo="true" :style="{ width: state.listBtnType == 6 ? '78%' : '100%' }" ref="zujianname"
      :options="state.options">
      <template v-slot:opraCurdBtnlist="row">
        <span>
          <el-button class="tableFontSize" type="primary" link size="small" @click="lookBtn(row.row.row)">填写分析
          </el-button>
          <el-button class="tableFontSize" type="primary" link size="small" @click="editBtn(row.row.row)"
            v-if="row.row.row.templateDto != null">查看分析
          </el-button>
        </span>
      </template>
      <template v-slot:selectNamelist> 实时传感器使用年限</template>
      <template v-slot:listSS="{ row, field }">
        <div v-if="field == 'sensorLife'"
        :style="{color:row.sensorLife=='使用期限内'?'green':
        row.sensorLife=='超出使用期限半年内'?'yellow':
        row.sensorLife=='超出使用期限半年外'?'orange':'red'}">
          {{row.sensorLife}}
        </div>
      </template>
    </list_controller>
  </div>
</template>
<script setup>
  import list_controller from "@/components/commonCom/list_controller";
  import {
    computed,
    reactive,
    ref,
  } from "vue";
  import {
    table_headers
  } from "@/components/commonCom/table_headers";
  import axios from "@/utils/request";
  import usePagesStore from "@/store/modules/pages";
  import dayjs from "dayjs";
  import {
    getlistSensorName
  } from '@/api/config'
  // 时间插件
  import duration from 'dayjs/plugin/duration'
  dayjs.extend(duration);
  const {
    proxy
  } = getCurrentInstance();
  const store = usePagesStore();
  const router = useRouter();
  let state = reactive({
    detailsShow: false,
    detailsList: [],
    fillayaComShow: false, //控制填写分析抽屉显示
    listBtnType: undefined,
    options: {
      refreshShow: true,
      tableheads: computed(() => table_headers.realSensorLife),
      listSS: [{
        field: "sensorLife"
      }],
      queryList: {
        querys: [{
            field: "childIndexName",
            title: "设备名称",
            type: "cascader",
            binddata: "",
            options: [],
            cascader_props: {
              children: "children",
              value: "name",
              label: "name",
              emitPath: false,
            },
          },
          {
            field: "sensorLife",
            title: "使用状态",
            type: "select",
            binddata: "",
            options: [{
              label: '使用期限内',
              value: '使用期限内'
            }, {
              label: '超出使用期限半年内',
              value: '超出使用期限半年内'
            }, {
              label: '超出使用期限半年外',
              value: '超出使用期限半年外'
            }, {
              label: '超出最长使用期限',
              value: '超出最长使用期限'
            }],
          },
        ],
      },
      dataUrl: "/realSensorLife/list",
    },
    tempList: [],
    timeDiff: '',
    // 表单
    editId: undefined,
    dataOption: {
      twoTitle: '近24小时异常统计',
      threeTitle: '馈电异常次数及时长统计',
      fourTitle: '馈电测点异常次数',
      twoUrl: '/realPointAlarm/attainAnomalyInfo',
      threeUrl: '/hisPointFeed/listFeedInfoStatistic',
      fourUrl: '/hisPointFeed/listFeedStatistic'
    },
    drawerShow: false,
    pointDataList: [],
  });

  // 填写分析抽屉
  function lookBtn(row) {
    state.editId = row.id;
    state.fillayaComShow = dayjs().unix();
  }
  // 编辑按钮
  function editBtn(row) {
    state.detailsShow = dayjs().unix();
    state.detailsList = row.templateDto;
  }
  // 计算累计时间
  let formattedTime = ref('')
  const computTime = (row) => {
    if (row.durationTime) {
      setInterval(() => {
        const now = new Date(); // 当前时间
        const target = new Date(row.startTime); // 目标时间
        const diff = Math.abs(now - target); // 时间差（毫秒）
        const days = Math.floor(diff / (1000 * 60 * 60 * 24));
        const hours = Math.floor((diff / (1000 * 60 * 60)) % 24);
        const minutes = Math.floor((diff / (1000 * 60)) % 60);
        const seconds = Math.floor((diff / 1000) % 60);
        row.durationTime = `${days}天${hours}时${minutes}分${seconds}秒`;
      }, 1000);
    } else {
      const now = new Date(); // 当前时间
      const target = new Date(row.startTime); // 目标时间
      const diff = Math.abs(now - target); // 时间差（毫秒）
      const days = Math.floor(diff / (1000 * 60 * 60 * 24));
      const hours = Math.floor((diff / (1000 * 60 * 60)) % 24);
      const minutes = Math.floor((diff / (1000 * 60)) % 60);
      const seconds = Math.floor((diff / 1000) % 60);
      row.durationTime = `${days}天${hours}时${minutes}分${seconds}秒`;
    }
    return row.durationTime
  }
  // 关闭统计数据
  const close = () => {
    state.listBtnType = undefined
  }
  // 跳转历史馈电异常页面
  const goPage = () => {
    router.push({
      name: 'Cx_hisLJBJList'
    })
  }

  function getSelList() {
    getlistSensorName({}).then((res) => {
      state.options.queryList.querys[0].options = res.data;
    });
  }
  getSelList()
</script>
<style lang="scss" scoped>
  .xiazai {
    position: absolute;
    top: 42px;
    right: 241px;
    width: 120px;
    height: 64px;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: space-between;
    background-color: #ffff;
    box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
    border-radius: 2px;
    z-index: 99;

    .item {
      width: 100%;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
    }

    .item:hover {
      background-color: rgba(91, 109, 128, 0.08);
    }
  }

  .pagestyle {
    display: flex;
    justify-content: space-between;
  }
</style>